import time

from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By

# 指定路径
service = Service(executable_path='D:/qiku/level4/chromedriver.exe')
# 选择浏览器
driver = webdriver.Chrome(service=service)

driver.execute_script("""
(function() {
    // 防止重复初始化
    if (window.mouseTracker) return;
    window.mouseTracker = true;

    // 创建样式
    const style = document.createElement('style');
    style.textContent = `
        #cursor-tracker-dot {
            position: fixed;
            width: 12px;
            height: 12px;
            background-color: #ff0000;
            border-radius: 50%;
            pointer-events: none;
            transform: translate(-50%, -50%);
            z-index: 9999;
            transition: all 0.05s ease-out;
        }
        #cursor-position-info {
            position: fixed;
            top: 20px;
            left: 20px;
            background: rgba(0, 0, 0, 0.8);
            color: white;
            padding: 10px 15px;
            border-radius: 4px;
            font-family: monospace;
            font-size: 14px;
            z-index: 10000;
            user-select: none;
        }
    `;
    document.head.appendChild(style);

    // 创建小红点
    const dot = document.createElement('div');
    dot.id = 'cursor-tracker-dot';
    document.body.appendChild(dot);

    // 创建坐标显示
    const info = document.createElement('div');
    info.id = 'cursor-position-info';
    info.innerHTML = 'X: <span id="cursor-x">0</span>px | Y: <span id="cursor-y">0</span>px';
    document.body.appendChild(info);

    // 监听鼠标移动
    document.addEventListener('mousemove', (e) => {
        const x = e.clientX;
        const y = e.clientY;
        dot.style.left = x + 'px';
        dot.style.top = y + 'px';
        document.getElementById('cursor-x').textContent = x;
        document.getElementById('cursor-y').textContent = y;
    });
})();""")

driver.get('https://www.chaojiying.com/user/login/')

#截取正页
# driver.get_screenshot_as_file('./tu/zdh/chaojiying.png')

# 指定元素截取
element = driver.find_element(By.CLASS_NAME, 'login_img') # 百度按钮
element.screenshot('./tu/zdh/element.png')


time.sleep(5)
driver.quit()